﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Wpf_ClientLourd.ServiceConnexion;
using Wpf_ClientLourd.BusinessObject;

namespace Wpf_ClientLourd
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ServiceConnexionClient cli;
        Parametres param;

        public MainWindow()
        {
            cli = new ServiceConnexionClient();
            InitializeComponent();
        }

        private void bt_quitter_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void bt_co_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result;

            //Appel du webservice pour la connexion et l'instanciation de notre User
            try
            {
                User user = new User(cli.ConnectUser(this.txtBox_login.Text, this.passwordBox1.Password));

                if (user.idUser != 0)
                {
                    MenuPrincipal menu = new MenuPrincipal(user, param);
                    menu.Show();
                    this.Close();
                }
                else
                {
                    this.txtBox_login.Text = "";
                    this.passwordBox1.Password = "";
                    throw new Exception("Login ou mot de passe incorrect !!");
                }
            }
            catch (System.ServiceModel.EndpointNotFoundException ex)
            {
                result = MessageBox.Show("Erreur de connexion avec le webservice", "Erreur de connexion", MessageBoxButton.OK, MessageBoxImage.Error);
                Console.WriteLine(ex.Message);
                Console.Read();
            }
            catch (Exception e1)
            {
                result = MessageBox.Show(e1.Message, "Echec de l'authentification", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        //au chargement de la fenêtre
        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            //Chargement de la table paramètres
            param = new Parametres(cli.ChargeParam());

            //focus sur le champ login
            FocusManager.SetFocusedElement(this, this.txtBox_login);
        }
        /// <summary>
        /// Méthode permettant de valider si on appuie sur la touche "Entree"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnKeyDownEnter(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                bt_co_Click(sender, e);
            }
        }

        }
}
